package com.blog.config;

import com.blog.aop.MyStartupRunner;
import com.blog.common.Cons;
import com.blog.filter.LoginHandlerInterceptor;

import com.blog.util.ToolUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author jiafujun
 * 2020年01月08日 18:02:00
 */
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {

    @Autowired
    private LoginHandlerInterceptor loginHandlerInterceptor;

    private static final String[] exPath = {"/**/login", "/blog/**/*", "/**/reg", "/comm/code", "/swagger/*",
            "/article/getData", "/article/getByInfo", "/article/browse", "/article/praise", "/**/about", "/discuss/**",
            "/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg",
            "/**/*.xml", "/**/*.xsl",
            "/**/*.jpeg", "/**/*.gif", "/**/fonts/*"};

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/blog/ad/login").setViewName("login");
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginHandlerInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(exPath)
                .excludePathPatterns("/static/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
        if (ToolUtil.getSystemType()) {
            registry.addResourceHandler("/OTA/**").addResourceLocations("file:" + Cons.STORAGE_PATH);
        } else {
            registry.addResourceHandler("/OTA/**").addResourceLocations("file:" + Cons.STORAGE_LIUNX_PATH);
        }
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }
}
